Count the frequency of words in a fileΒΆ
Count the frequency of words in a file.
from collections import Counter
def word_count(fname):
with open(fname) as f:
return Counter(f.read().split())
# test
print("Number of words in the file :", word_count("test.txt"))
Output:
Number of words in the file : \
Counter({'this': 7, 'Append': 5, 'text.': 5, \
'text.Append': 2, 'Welcome': 1, 'to': 1, 'w3resource.com.': 1})